package com.zhang.dao;


import com.zhang.pojo.User;
import com.zhang.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.type.Alias;
import org.junit.Test;

import java.util.List;

@Alias("User")
public class UserTestDao {

    @Test
    public void Test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    @Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(1);
        System.out.println(user.toString());
    }

    @Test
    public void addUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int id = mapper.addUser(new User(5,"张国荣","123456"));
        System.out.println(id);
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUser(new User(4,"嘿嘿","123456"));
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void deleteUserById(){
        // 获取会话
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        // 获取接口服务类对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        // 调用服务接口
        mapper.deleteUSer(5);
        sqlSession.commit();
        sqlSession.close();
    }


}
